<?php
/**
 * User:
 * Date:
 * Time:
 */

namespace App\Common\RedisCache;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

Abstract class AbstractStringCache extends AbstractCache
{
    /**
     * 是否json处理
     * @var bool
     */
    protected static $json = false;

    public function set($value, $ttl = 0)
    {
        $this->select();

        if (is_array($value)) {
            $value = json_encode($value);
        }
        if ($ttl) {
            $r = $this::$client->set($this->key, $value, $ttl);
        } else {
            $r = $this::$client->set($this->key, $value);
        }

        return $r;
    }

    public function get($key = '')
    {
        $this->select();

        if (empty($key)) {
            $key = $this->key;
        }
        $r = $this::$client->get($key);
        $this->raw_data = $r;

        if ($this::$json) {
            $r = json_decode($r, true);
        }

        return $r;
    }

    public function len($key = ''): int
    {
        $this->select();

        if (empty($key)) {
            $key = $this->key;
        }

        return $this::$client->strlen($key);
    }

    /**
     * String值+1
     * @param string $key
     * @return int
     */
    public function increase($key = '')
    {
        $this->select();

        if (empty($key)) {
            $key = $this->key;
        }

        return $this::$client->incr($key);
    }

    /**
     * String值-1
     * @param string $key
     * @return int
     */
    public function decrease($key = '')
    {
        $this->select();

        if (empty($key)) {
            $key = $this->key;
        }

        return $this::$client->decr($key);
    }

    /**
     * String值整型增量
     * @param $value
     * @param string $key
     * @return int
     */
    public function increaseByInt($value, $key = '')
    {
        $this->select();

        if (empty($key)) {
            $key = $this->key;
        }
        $value = intval($value);

        return $this::$client->incrBy($key, $value);
    }

    /**
     * String值浮点型增量
     * @param $value
     * @param string $key
     * @return float
     */
    public function increaseByFloat($value, $key = '')
    {
        $this->select();

        if (empty($key)) {
            $key = $this->key;
        }
        $value = floatval($value);

        return $this::$client->incrByFloat($key, $value);
    }

    /**
     * @param $value
     * @param $ttl
     * @param string $key
     * @return bool
     */
    public function setEx($value, $ttl, $key = '')
    {
        $this->select();

        if (empty($key)) {
            $key = $this->key;
        }
        if (is_array($value)) {
            $value = json_encode($value);
        }

        return $this::$client->setex($key, $ttl, $value);
    }

    /**
     * @param $value
     * @param $ttl
     * @param string $key
     * @return bool
     */
    public function setNx($value, $ttl, $key = '')
    {
        $this->select();

        if (empty($key)) {
            $key = $this->key;
        }
        if (is_array($value)) {
            $value = json_encode($value);
        }

        return $this::$client->setnx($key, $ttl, $value);
    }

    /**
     * String值整型减量
     * @param $value
     * @param string $key
     * @return int
     */
    public function decreaseByInt($value, $key = '')
    {
        $this->select();

        if (empty($key)) {
            $key = $this->key;
        }
        $value = intval($value);

        return $this::$client->decrBy($key, $value);
    }

}
